package com.seecen.server.controller;


import com.seecen.server.dao.IUserDao;
import com.seecen.server.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin   // 允许跨域
@RequestMapping("/api")
public class UserController {
    @Autowired
    private IUserDao iUserDao;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @PostMapping("/register")    // 前端调用的地址
    public int register(@RequestBody User user) {
        return iUserDao.save(user);
    }
    @PostMapping("/login")    // 前端调用的地址
    public String login(@RequestBody User user) {
        User user1 = iUserDao.findByUsername(user.getUsername());
        System.out.println(user1);
        if(user1 == null || !user1.getPassword().equals(user.getPassword())){
            return null;
        }
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(token,user1.getUsername(),30, TimeUnit.SECONDS);
        return token;
    }
}
